package com.rtsffm.rtd.test;

import com.rtsffm.rtd.action.controller.ActionDelegate;
import com.rtsffm.rtd.action.model.annotations.ActionFrameworkEnabled;

import org.junit.Test;

//~--- JDK imports ------------------------------------------------------------


import java.awt.BorderLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.annotation.Annotation;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

//~--- classes ----------------------------------------------------------------

@ActionFrameworkEnabled
public class CheckComponentIsShowing extends JPanel implements ActionDelegate {
    private static final long serialVersionUID = 1L;

    //~--- fields -------------------------------------------------------------

    private final JFrame frame = new JFrame();;

    //~--- constructors -------------------------------------------------------

    public CheckComponentIsShowing() {
        super(new BorderLayout());

        JTextField f = new JTextField(10);

        f.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentHidden(ComponentEvent e) {
                super.componentHidden(e);
            }
        });
        f.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println(evt);
            }
        });
        add(f);
    }

    //~--- methods ------------------------------------------------------------

    public void testM(Object object, Object... objects) {
        System.out.println(objects);
    }

    private void startGui() {
        System.out.println(getFormatedStackTrace());
        testM(new Object());
        testM(new Object(), null, null);
        testM(new Object(), new Integer(1));
        testM(new Object(), new Integer(1), new Float(1));

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                WindowAdapter wa = new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent e) {
                        System.out.println(CheckComponentIsShowing.this.isShowing());
                    }

                    @Override
                    public void windowClosing(WindowEvent e) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                System.out.println(CheckComponentIsShowing.this.isShowing());
                            }
                        });
                    }

                    @Override
                    public void windowStateChanged(WindowEvent e) {
                        System.out.println(CheckComponentIsShowing.this.isShowing());
                    }
                };

                frame.setContentPane(CheckComponentIsShowing.this);
                frame.addWindowListener(wa);
                frame.addWindowStateListener(wa);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    @Test
    public void testIsShowingOnDispose() throws InterruptedException {
        for (Annotation a : this.getClass().getAnnotations()) {
            System.out.println(a);
        }

        startGui();

        boolean isShowing = false;

        while (!isShowing) {
            Thread.sleep(20);
            isShowing = frame.isShowing();
        }

        frame.dispose();
    }

    //~--- get methods --------------------------------------------------------

    private static String getFormatedStackTrace() {
        StringBuilder b = new StringBuilder();

        b.append("Thread:").append(Thread.currentThread().getName() + "\n");

        StackTraceElement[] elements = new RuntimeException().getStackTrace();

        for (int i = 1; i < elements.length; i++) {
            StackTraceElement elem = elements[i];

            b.append(elem.getClassName()).append("#").append(elem.getMethodName()).append(":").append(
                elem.getLineNumber()).append("\n");
        }

        return b.toString();
    }

    @Override
    public boolean isStillValid() {
        return true;
    }
}
